/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.elcom.emocbc.cbcgateway;

import com.elcom.config.ModuleConfig;
import com.elcom.emocbc.cbcgateway.ipc.CbcGatewayServerTrap;
import com.elcom.util.Constant;
import com.elcom.util.app.IApplication;
import com.elcom.util.app.IPCHandlerFactory;
import com.elcom.util.ipc.IPCHandler;
import java.io.File;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import org.apache.log4j.PropertyConfigurator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author GiangLT
 */
public class CbcGateway implements IApplication {

    private final Logger logger = LoggerFactory.getLogger(CbcGateway.class);
    private ThreadPoolExecutor executor;
    private CbcGatewayServerTrap cbcGatewayServerTrap;
    private static CbcGateway instance = null;

    public static synchronized CbcGateway getInstance() {
        if (instance == null) {
            instance = new CbcGateway();
        }
        return instance;
    }
    
    private CbcGateway() {
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        CbcGateway cbcGateway = CbcGateway.getInstance();
        cbcGateway.start(args);
    }

    @Override
    public void stop(String[] args) {
    }

    @Override
    public void start(String[] args) {
        try {
            File file = new File(Constant.LOG_FOLDER);
            if (!file.exists()) {
                file.mkdirs();
            }

            file = new File(Constant.CONFIG_FOLDER);
            if (!file.exists()) {
                file.mkdirs();
            }

            PropertyConfigurator.configureAndWatch(Constant.CONFIG_FOLDER + "cbc-gateway-log4j.props");

            //Chay reload config
            ModuleConfig jConfig = new ModuleConfig();
            try {
                jConfig.reloadConfig(Constant.CONFIG_FOLDER, Constant.CONFIG_FILE_NAME);
            } catch (Exception ex) {
                logger.error("", ex);
            }

//            CbcHttpClient.getInstance().bind();//prestart HttpConnection
            
            cbcGatewayServerTrap = new CbcGatewayServerTrap(Constant.MODULE_NAME, Constant.CONFIG_FOLDER + "cbc-gateway-ipc-server.ini");
            cbcGatewayServerTrap.start();

            String moduleName = args[0];
            IPCHandler ipc = IPCHandlerFactory.createIPCHandler(moduleName);
            ModuleMonitor moduleMonitor = new ModuleMonitor(ipc);
            Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(moduleMonitor, 1, 1, TimeUnit.MICROSECONDS);

        } catch (Throwable ex) {
            logger.error("", ex);
        }
    }

    @Override
    public void restart(String[] args) {
    }

    @Override
    public String get_info(String[] args) {
        return "";
    }

    @Override
    public void reload_config(String[] args) {
    }

    public CbcGatewayServerTrap getCbcGatewayServerTrap() {
        return cbcGatewayServerTrap;
    }

    public void setCbcGatewayServerTrap(CbcGatewayServerTrap cbcGatewayServerTrap) {
        this.cbcGatewayServerTrap = cbcGatewayServerTrap;
    }

    public ThreadPoolExecutor getExecutor() {
        return executor;
    }

    public void setExecutor(ThreadPoolExecutor executor) {
        this.executor = executor;
    }

    @Override
    public String get_name() {
        return Constant.MODULE_NAME;
    }

    @Override
    public void set_name(String string) {
    }

    @Override
    public int get_index() {
        return -1;
    }

    @Override
    public void set_index(int i) {
    }
}
